<?php
Globals::requireController('Admin');
Globals::requireTable('Product');
Globals::requireTable('Days');
Globals::requireTable('Account');
Globals::requireTable('Order');
Globals::requireTable('Vlevel');
Globals::requireTable('Msg');

class AccountController extends AdminController
{
    protected $productTable;
    protected $daysTable;
    protected $accountTable;
    protected $orderTable;
    protected $vlevelTable;
    protected $msgTable;
    
	public function __construct($config = null)
	{
		parent::__construct($config, true);
		$this->productTable = new ProductTable($config);
		$this->daysTable = new DaysTable($config);
		$this->accountTable = new AccountTable($config);
		$this->orderTable = new OrderTable($config);
		$this->vlevelTable = new VlevelTable($config);
		$this->msgTable = new MsgTable($config);
	}
	
	public function indexAction()
	{
	    $where['status'] = 1;
	    $count = $this->productTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->productTable->listPage($where, 'id ASC', $pageSize, $pageId);
	}
	
	public function addAction()
	{
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $product = $this->productTable->getRow($id);
	        if($product['id'] > 0 && $product['status'] == 1){
	            $this->view->product = $product;
	        }
	    }
	}
	
	public function doaddAction()
	{
	    $id = $this->getIntParam('id');
	    $data['name'] = $this->getParam('name1');
	    $data['name_1'] = $this->getParam('name2');
	    $data['name_2'] = $this->getParam('name3');
	    $data['img'] = $this->getParam('img');
	    $data['img_1'] = $this->getParam('img2');
	    $data['img_2'] = $this->getParam('img3');
	    $data['rank'] = $this->getParam('rank');
	    $data['needs'] = $this->getParam('needs');
	    $data['createdate'] = date('Y-m-d H:i:s');
	    if($id > 0){
	        $this->productTable->update($data,$id);
	    }else{
	        $this->productTable->add($data);
	    }
	    $this->redirectToAction();
	    exit();
	}
	
	public function deleteAction()
	{
	    $id = $this->getIntParam('id');
	    $this->productTable->update(array('status'=>0),$id);
	    $this->redirectToAction();
	    exit();
	}
	
	public function accountAction()
	{
	    $pid = $this->getIntParam('pid');
	    if($pid > 0){
	        $where['pid'] = $pid;
	    }
	    
	    $uid = $this->getIntParam('uid');
	    if($uid > 0){
	        $where['uid'] = $uid;
	    }
	    
	    $status = $this->getIntParam('status');
	    $status = $status == 0 ? 1 : $status;
	    if($status > 0){
	        $where['status'] = $status;
	    }
	    //var_dump($where);exit();
	    $count = $this->accountTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->accountTable->listPage($where, 'id DESC', $pageSize, $pageId);
	    $this->view->product = $this->productTable->listAll(array('status'=>1));
	    $this->view->pid = $pid;
	    $this->view->uid = $uid;
	    $this->view->status = $status;
	    
	    global $account_status;
	    $this->view->account_status = $account_status;
	}
	
    public function accountaddAction()
	{
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $account = $this->accountTable->getRow($id);
	        if($account['id'] > 0){
	            $this->view->account = $account;
	            $time = time();
	        }
	    }
	    $this->view->product = $this->productTable->listAll(array('status'=>1));
	}
	
	public function accountdoaddAction()
	{
	    $id = $this->getIntParam('id');
	    $status = $this->getIntParam('status');
	    $data['pid'] = $this->getIntParam('pid');
	    $data['username'] = $this->getParam('username');
	    $data['nums_show'] = $this->getParam('nums_show');
	    $data['nums_value'] = $this->getIntParam('nums_value');
	    if($status == 2 || $status == 3){
	        $data['status'] = $status;
	        $data['nums_stock'] = 0;
	    }
	    //$data['createdate'] = date('Y-m-d H:i:s');
	    $account = $this->accountTable->getRow($id);
	    if($account['id'] > 0){
	        $this->accountTable->update($data,$id);
	    }else{
	        $this->accountTable->add($data);
	    }
	    
	    if($data['status'] == 3){
	        $account = $this->accountTable->getRow($id);
	        $vlist_data['uid'] = $account['uid'];
	        $vlist_data['level'] = -1;
	        $vlist_data['type'] = 3;
	        $vlist_data['remark'] = "您提交的试用账号（{$account['username']}）密码错误或不是对应平台的会员账号，未能通过人工审核，并将扣除您1V的信用等级，如有任何疑问请进入APP首页里的用户中心点击下方的红色按钮反馈。";
	        $this->vlevelTable->add($vlist_data);
	        
	        $msg_data['uid'] = $account['uid'];
	        $msg_data['createdate'] = date('Y-m-d H:i:s');
	        $msg_data['msg'] = "您提交的试用账号（{$account['username']}）错误或不是对应平台的会员账号，未能通过人工审核，并将扣除您1V的信用等级，如有任何疑问请进入APP首页里的用户中心点击下方的红色按钮反馈。";
	        $msg_data['status'] = 0;
	        $this->msgTable->add($msg_data);
	        
	        require_once './jpush/autoload.php';
	        $client = new \JPush\Client(DOMAIN_JPUSH_APP_KEY, DOMAIN_JPUSH_MASTER_SECRET);
	        try {
	            $exreas = array('notpassaudit'=>'您的信用被扣除了1V级！');
	            $re = $client->push()
	            ->setPlatform('all')
	            ->addAlias($account['uid'])
	            ->addIosNotification('账号未通过审核','default',1,false,'',$exreas)
	            ->addAndroidNotification('账号未通过审核',NULL,0,$exreas)
	            ->send();
	            dolog('ok-'.json_encode($re));
	        } catch (\JPush\Exceptions\APIConnectionException $e) {
	            dolog('err1-'.json_encode($e));
	        } catch (\JPush\Exceptions\APIRequestException $e) {
	            dolog('err2-'.json_encode($e));
	        }
	    }elseif($data['status'] == 2){
	        $msg_data['uid'] = $account['uid'];
	        $msg_data['createdate'] = date('Y-m-d H:i:s');
	        $msg_data['msg'] = "您提供的试用账号已通过人工审核，有用户购买就可以赚钱了！";
	        $msg_data['status'] = 0;
	        $this->msgTable->add($msg_data);
	    }
	    
	    $this->redirectToAction('account');
	    exit();
	}
	
	public function accountdeleteAction()
	{
	    $id = $this->getIntParam('id');
	    $this->accountTable->update(array('status'=>0),$id);
	    $this->redirectToAction('account');
	    exit();
	}
	
	public function updatepasswordAction(){
	    $id = $this->getIntParam('id');
	    $account = $this->accountTable->getRow($id);
	    $this->view->account = $account;
	    if($account['id'] < 1){
	        $this->redirectToAction('account');
	        exit();
	    }
	}
	
	public function doupdatepasswordAction(){
	    $id = $this->getIntParam('id');
	    $data['password'] = $this->getParam('password');
	    $data['endtime'] = 0;
	    $data['status'] = 2;
	    $data['nums_stock'] = 0;
	    $this->accountTable->update($data,$id);
	    $this->redirectToAction('account');
	    exit();
	}
	
}
